<?php

require_once BaseUtil::getModulePath ("base") . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath ("config") . '/services/EmailTemplateService.php';
require_once BaseUtil::getModulePath ("config") . '/forms/EmailTemplateForm.php';

class Config_EmailtemplateController extends BaseController {
	
	public function init() {
		parent::init();
	}
	
	public function indexAction() {
		$emailtemplateService = new EmailTemplateService();
		$template = $emailtemplateService->getTemplate($this->view,$_POST);
		$this->view->template = $template;
	}
	
	public function addAction() {
		$emailtemplateForm = new EmailTemplateForm();
		$this->view->form = $emailtemplateForm;
	}
	
	public function subaddAction() {		
		$emailtemplateForm=new EmailTemplateForm();
		$emailtemplateService=new EmailTemplateService();
		if(! $emailtemplateForm->isValid($_POST)) {
			$this->view->form=$emailtemplateForm;
			return $this->render('add');			
		}
		$emailtemplateService->addTemplate($emailtemplateForm);
		$this->_redirect('/config/emailtemplate');
	}
	
	public function editAction() {
		$templateId = $this->_getParam('id');
		$emailtemplateService = new EmailTemplateService();
		$template = $emailtemplateService->getTemplateById($templateId);
		$emailtemplateForm = new EmailTemplateForm(true);
		$emailtemplateForm->getElement("id")->setValue($template['id']);
		$emailtemplateForm->getElement("subject")->setValue($template['subject']);
		$emailtemplateForm->getElement("sender")->setValue($template['sender']);
		$emailtemplateForm->getElement("tag")->setValue($template['tag']);
		$emailtemplateForm->getElement("content")->setValue($template['content']);
		$this->view->form = $emailtemplateForm;
		$this->view->id = $templateId;
	}
	
	public function subeditAction() {
		$id = HttpUtil::getPostString("id");
		$emailtemplateForm = new EmailTemplateForm(true,$id);
		$emailtemplateService = new EmailTemplateService();
		if (!$emailtemplateForm->isValid($_POST)) {
			$this->view->form = $emailtemplateForm;
			return $this->render('edit');
		}
		$emailtemplateService->editTemplate($emailtemplateForm);
		$this->_redirect('/config/emailtemplate');
	}
	
	public function deletesomeAction() {	
		$idsStr=HttpUtil::getPostValue("selectIds");
		$ids=explode(",",$idsStr);
		$emailtemplateService=new EmailTemplateService();
		$emailtemplateService->deleteTemplate($ids);
		$this->_redirector->gotoUrl('/config/emailtemplate');
	}
	
	public function deleteAction() {
		$id=$this->_getParam('id');       
        $emailtemplateService=new EmailTemplateService();
        $emailtemplateService->delete($id);
        $this->_redirector->gotoUrl('/config/emailtemplate');
	}
}
